package com.example.securitytest.service.impl;

import com.example.securitytest.domain.Item;
import com.example.securitytest.mapper.ItemMapper;
import com.example.securitytest.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Override
    public List<Item> getAllItems() {
        return itemMapper.selectList(null);
    }

    @Override
    public Item getItemByName(String name) {
        return itemMapper.selectByName(name);
    }

    @Override
    public Item createItem(Item item) {
        itemMapper.insert(item);
        return item;
    }

    @Override
    public Item updateItem(String name, Item item) {
        Item existingItem = getItemByName(name);
        if (existingItem != null) {
            itemMapper.updateById(item);
            return item;
        } else {
            throw new RuntimeException("Item not found with name: " + name);
        }
    }

    @Override
    public void deleteItem(String name) {
        itemMapper.deleteByName(name);
    }

    @Override
    public List<Item> uploadItems(List<Item> items) {
        itemMapper.insertBatchSomeColumn(items);
        return items;
    }

    @Override
    public List<Item> getItemsByPriceRange(double minPrice, double maxPrice) {
        return itemMapper.selectByPriceBetween(minPrice, maxPrice);
    }
}

